﻿app.controller('ReserveOrdersCtrl', ['$scope', '$http', '$filter','httpPlus','utils','$state',function ($scope, $http, $filter,httpPlus,utils,$state) {
    utils.setTitle('订单中心');
    var BILLTYPEDic = { TangShi: "店内消费", WaiMai: "外卖", YuDian: "预点", ZiTi: "自提" };
    var BILLSTATUSDic = { "0": "未下单", "1": "待复单", "2": "等待出单", "3": "订单已关闭", "4": "订单已完成", "5": "出单异常" };
    var PAYTYPEDic = { "0": "会员卡支付", "1": "现金支付", "2": "微信支付", "3": "线下支付" };
    $scope.data = {
        isShowDetail :false,
        tabs : [
            {
                "name": "全部",
                "value":1,
            },
            {
                "name": "预定",
                "value": 2,
            },
            {
                "name": "外卖",
                "value": 3,
            },
            {
                "name": "堂食",
                "value": 4,
            }
        ],
        currTab : undefined,
        ordersList:[],
        currOrder:undefined,
        wechatOrderNewVersion180125:false,
        orderDetails : {},

    };
    $scope.data.currTab = $scope.data.tabs[0].value;
    $scope.event = {
        btnChooseTab_click:function (val) {
            $scope.data.currTab = val.value;
        },
        editOrder_click:function(){
            $state.go('wx.reserveOrder',{'billid': $scope.data.currOrder.GUID,'code':$scope.data.currOrder.LID});
        },
        payOrder_click:function(){
            $state.go('wx.pay_bill',{'billid': $scope.data.currOrder.GUID,'code':$scope.data.currOrder.LID});
        },
        orderDetail_click:function(item){
            console.log(item);
            $scope.data.isShowDetail = true;
            $scope.data.currOrder = item;
            httpPlus
                .MMB2RRRApi('MBBillHis/GetMyBillDetail',{
                         _billGuid:item.GUID
                    })
                        .then(function (data) {
                            console.log(data);
                            $scope.data.orderDetails.BillItems = data.BillItems;
                        });
        }
    }

    httpPlus
        .MMB2RRRApi('MBBillHis/InitMyBillsPage')
        .then(function (data) {
            console.log(data);
            $scope.data.wechatOrderNewVersion180125 = data.wechatOrderNewVersion180125;
            $scope.data.ordersList = _.map(data.Bills,function(item){
                item.lblTopStatusView = BILLSTATUSDic[item.BillStatus];
                if (!item.IsPay && item.IsPay == 3 && item.BillStatus == 4) item.lblTopStatusView = "进行中";
                if(item.BillStatus != 3 && !item.IsPay) item.lblTopStatusView_Css = 'show';
                else item.lblTopStatusView_Css = 'hidden';
                item.lblBotStatusView = BILLSTATUSDic[item.BillStatus];
                item.lblBotStatusView_Css = 'hidden';
                if(item.BillStatus == 3) item.lblBotStatusView_Css = 'show';
                if(item.BillStatus == 4 && item.IsPay) item.lblBotStatusView_Css = 'show';
                item.payTypeText = PAYTYPEDic[item.PayType];
                if (item.PayType == "3")  item.payTypeText =  "线下支付";
                if (item.IsPay == "0")  item.payTypeText = "未支付";
                item.btnUpdBill_Css = true;
                if(item.BillStatus == 3) item.btnUpdBill_Css = false;
                if (item.IsPay || item.PayType == '') item.btnUpdBill_Css = false;
                item.btnPayBill_Css = item.IsPay?false:true;
                if(item.BillStatus == 3)item.btnPayBill_Css = false;
                return item;
            });
            console.log( $scope.data.ordersList);

        });
    console.log('$scope.ordersList', $scope.ordersList);
}]);